/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xbuffer.h
 *
 * Purpose: data buffer type define 
 *
 * Developer:
 *   wen.gu , 2023-08-09
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_CORE_XBUFFER_H__
#define __XBUS_CORE_XBUFFER_H__

#include <memory>
#include <vector>
#include <cstring>

#define XBUS_BUFFER_DEFAULT_CAPACITY (32)
namespace xbus::core {

#if 1
class XBuffer {
public:
    explicit XBuffer(uint32_t capacity = XBUS_BUFFER_DEFAULT_CAPACITY)
    :capacity_(capacity),
    data_(new uint8_t[capacity]) {
        /** todo something */
    }

    virtual ~XBuffer() { /** todo something*/ }

public:
    virtual uint8_t* data() noexcept { return data_.get(); }
    virtual const uint8_t* data() const { return data_.get(); }
    virtual uint32_t size() const { return size_; }
    virtual uint32_t capacity() const { return capacity_; }

    virtual bool set_data(const uint8_t* data, uint32_t size, uint32_t start_pos = 0) {
        if (start_pos > size_) { return false; }
        size_ = start_pos;
        return adppend_data(data, size);
    }

    virtual bool assign(uint32_t size) {
        if (resize(size)) {
            size_ = size;
            return true;
        }

        return false;
    }


    virtual bool adppend_data(const uint8_t* data, uint32_t size) {
        if ((size_ + size) > capacity_) {
            uint32_t new_size = std::max(capacity_ + (capacity_ / 2), (size_ + size));
            if (!resize(new_size)) { return false;  }
        }

        memcpy(data_.get() + size_, data, size);
        size_ += size;
        return true;
    }

    virtual void clear() { size_ = 0; }
    virtual bool resize(uint32_t new_capacity) {
        if (new_capacity < capacity_) {
            return true;
        }

        std::unique_ptr<uint8_t[]> new_data(new uint8_t[new_capacity]);
        memcpy(new_data.get(), data_.get(), size_);
        data_ = std::move(new_data);
        capacity_ = new_capacity;
        return true;
    }

public:
    template<typename T, typename std::enable_if<(std::is_integral<T>::value ||  std::is_floating_point<T>::value) >::type >
    bool adppend_data(T val) {
        return adppend_data(&val, sizeof(T));
    }


protected:
    uint32_t size_ = 0;
    uint32_t capacity_;
    std::unique_ptr<uint8_t[]> data_;
};
#else 
using XBuffer = std::vector<uint8_t>;
#endif

using XBufferPtr = std::shared_ptr<XBuffer>;

} //namespace xbus::core


#endif /** !__XBUS_CORE_XBUFFER_H__ */